﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class CMS_Admin_block_CommonInfo : System.Web.UI.Page
{
    Usas.BLL.Cms.cms_block cb = new Usas.BLL.Cms.cms_block();
    Usas.BLL.Cms.cms_block_common_detail cbcd = new Usas.BLL.Cms.cms_block_common_detail();
    Usas.BLL.Cms.cms_block_common_detail_option cop = new Usas.BLL.Cms.cms_block_common_detail_option();

    Usas.Model.Cms.cms_block_common_detail cbcdm = new Usas.Model.Cms.cms_block_common_detail();
    Usas.Model.Cms.cms_block_common_detail_option copm = new Usas.Model.Cms.cms_block_common_detail_option();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            OptionBind();
        }
    }

    private void OptionBind()
    {
        DataSet ds = cbcd.GetList("c_b_id=" + Request["control"].ToString() + " and c_type='" + Request["page"].ToString() + "' and c_view != 0");
        RptList.DataSource = ds;
        RptList.DataBind();

        addoption.Visible = false;
    }

    protected void RptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            LinkButton see = (LinkButton)e.Item.FindControl("see");
            see.CommandName = "see";
            see.CommandArgument = drv["c_value"].ToString();

            LinkButton Edit = (LinkButton)e.Item.FindControl("Edit");
            Edit.CommandName = "edit";
            Edit.CommandArgument = drv["c_id"].ToString() + "|" + drv["c_des"].ToString() + "|" + drv["c_value"].ToString();
        }
    }

    protected void option_edit_Click(object sender, EventArgs e)
    {
        if (Fu.HasFile)
        {
            //string temp_path = Server.MapPath("~/UploadFile/CommonInfo/");
            string id = option_edit.CommandArgument.Split('|')[0];

            string _t = Path.GetExtension(Fu.FileName);

            string temp_path = Server.MapPath("~/UploadFile/CommonInfo/" + option_edit.CommandArgument.Split('|')[1]);

            Fu.SaveAs(temp_path);

            cbcdm.c_id = Convert.ToInt32(id);
            cbcdm.c_des = LitName.Text;
            cbcdm.c_value = option_edit.CommandArgument.Split('|')[1];
            cbcdm.c_view = 1;
            cbcdm.c_type = Request["page"].ToString();
            cbcdm.c_b_id = Convert.ToInt32(Request["control"]);
            cbcd.Update(cbcdm);

            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('更新成功');location.href='" + Request.Url.ToString() + "'", true);

        }
    }
    protected void option_cancel_Click(object sender, EventArgs e)
    {
        addoption.Visible = false;
    }
    protected void RptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "see")
        {
            string temp_path = Server.MapPath("~/UploadFile/CommonInfo/" + e.CommandArgument.ToString());
            if (File.Exists(temp_path))
            {
                FileInfo fn = new System.IO.FileInfo(temp_path);

                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fn.Name);
                HttpContext.Current.Response.WriteFile(fn.FullName);
                HttpContext.Current.Response.End();
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('文件不存在')", true);
            }
        }
        else if (e.CommandName == "edit")
        {
            EditOption(e.CommandArgument.ToString());
        }
    }

    private void EditOption(string id)
    {
        addoption.Visible = true;

        option_edit.Visible = true;

        option_edit.CommandArgument = id.Split('|')[0] + "|" +id.Split('|')[2];

        LitName.Text = id.Split('|')[1];
    }
}
